home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / dev / gcc / libnix.lha / gnu / lib / libnix / sources.lha / nixmain / __nocommandline.c next >
Encoding:
C/C++ Source or Header  |  1994-07-17  |  3.2 KB  |  141 lines

  1. #include <stdlib.h>
  2. #ifdef __GNUC__
  3. #include <inline/exec.h>
  4. #include <dos/dos.h>
  5. #include <inline/dos.h>
  6. #endif
  7. #ifndef __GNUC__
  8. #include <clib/exec_protos.h>
  9. #include <clib/dos_protos.h>
  10. #endif
  11. #include <exec/memory.h>
  12. #include <workbench/startup.h>
  13. #include <stabs.h>
  14.  
  15. extern int    __argc; /* Defined in startup */
  16. extern char **__argv;
  17. extern char  *__commandline;
  18. extern struct WBStartup *_WBenchMsg;
  19.  
  20. extern char __stdiowin[];
  21.  
  22. static char *cline=NULL; /* Copy of commandline */
  23. static BPTR cd=0l;       /* Lock for Current Directory */
  24. static BPTR window=0l;   /* CLI-window for start from workbench */
  25.  
  26. /* This guarantees that this module gets linked in.
  27.    If you replace this by an own reference called
  28.    __nocommandline you get no commandline arguments */
  29. ALIAS(__nocommandline,__initcommandline);
  30.  
  31. void __initcommandline(void)
  32. {
  33.   if(_WBenchMsg!=NULL)
  34.   { if(__stdiowin[0])
  35.     { if((window=Open(__stdiowin,MODE_OLDFILE))==0l)
  36.         exit(RETURN_FAIL);
  37.       SelectInput(window);
  38.       SelectOutput(window);
  39.     }
  40.     if(_WBenchMsg->sm_ArgList!=NULL) /* cd to icon */
  41.     { cd=DupLock(_WBenchMsg->sm_ArgList->wa_Lock);
  42.       CurrentDir(cd); }
  43.  
  44.     __argc=0;
  45.     __argv=(char **)_WBenchMsg;
  46.   }else
  47.   { 
  48.     size_t i;
  49.     char *a;
  50.  
  51.     for(i=0;__commandline[i]!='\n';i++) /* calculate size of commandline */
  52.       ;
  53.     cline=(char *)AllocVec(i+1,MEMF_ANY);
  54.   
  55.     if(cline==NULL)
  56.       exit(RETURN_FAIL);
  57.   
  58.     a=cline; /* and parse it */
  59.     __argc=1;
  60.     while(*__commandline!='\n')
  61.     {
  62.       if(*__commandline!='\"')
  63.         while(*__commandline!='\n'&&*__commandline!=' '&&*__commandline!='\t')
  64.           *a++=*__commandline++;
  65.       else
  66.       { __commandline++;
  67.         while(*__commandline!='\n'&&*__commandline!='\"')
  68.         {
  69.           if(*__commandline=='*')
  70.             if(*++__commandline=='\n')
  71.               break;
  72.           *a++=*__commandline++;
  73.         }
  74.       }
  75.       if(*__commandline!='\n')
  76.       {
  77.         __commandline++;
  78.         while(*__commandline==' '||*__commandline=='\t')
  79.           __commandline++;
  80.       }
  81.       *a++='\0';
  82.       __argc++;
  83.     }
  84.       /* NULL Terminated */
  85.     __argv=(char **)AllocVec((__argc+1)*sizeof(char *),MEMF_ANY|MEMF_CLEAR);
  86.     
  87.     if(__argv==NULL)
  88.       exit(RETURN_FAIL);
  89.     
  90.     a=cline;
  91.     for(i=1;i<__argc;i++)
  92.     { 
  93.       __argv[i]=a;
  94.       while(*a++)
  95.         ; 
  96.     }
  97.   
  98.     for(i=256;;i+=256) /* try in steps of 256 bytes */
  99.     { *__argv=(char *)AllocVec(i,MEMF_ANY);
  100.       if(*__argv==NULL)
  101.         break;
  102.       GetProgramName(*__argv,i); /* Who am I ? */
  103.       if(IoErr()!=ERROR_LINE_TOO_LONG)
  104.         break;
  105.       FreeVec(*__argv);
  106.     }
  107.   
  108.     if(*__argv==NULL)
  109.       exit(RETURN_FAIL);
  110.   }
  111. }
  112.   
  113. void __exitcommandline(void)
  114.   if(_WBenchMsg!=NULL)
  115.   { 
  116.     if(window!=0l)
  117.       Close(window);
  118.  
  119.     if(cd!=0) /* We locked a directory, it must be unlocked */
  120.     { CurrentDir(0l);
  121.       UnLock(cd); } /* the lock must not be used after it is unlocked!!!! */
  122.   }else
  123.   {
  124.     if(cline!=NULL)
  125.     { 
  126.       if(__argv!=NULL)
  127.       { 
  128.         if(*__argv!=NULL)
  129.           FreeVec(*__argv);
  130.         FreeVec(__argv);
  131.       }
  132.       FreeVec(cline);
  133.     }
  134.   }
  135. }
  136.   
  137. /* Add these two functions to the lists */
  138. ADD2INIT(__initcommandline,-40);
  139. ADD2EXIT(__exitcommandline,-40);
  140.